// order/order.entity.ts
import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn, OneToMany } from 'typeorm';
import { OrderDetail } from './order-detail.entity';

/**
 * 订单状态枚举
 */
export enum OrderStatusEnum {
  PENDING = 0,  // 待支付
  PAID = 1,     // 已支付
  MAKING = 2,   // 制作中
  COMPLETED = 3, // 已完成
  CANCELLED = 4  // 已取消
}

/**
 * 支付方式枚举
 */
export enum PaymentMethodEnum {
  WECHAT = 1,   // 微信支付
  ALIPAY = 2    // 支付宝
}

@Entity('orders')
export class Order {
  /**
   * 主键ID
   */
  @PrimaryGeneratedColumn()
  id: number;

  /**
   * 订单编号
   */
  @Column({ name: 'order_id', length: 50 })
  orderId: string;

  /**
   * 用户ID
   */
  @Column({ name: 'user_id' })
  userId: number;

  /**
   * 商家ID
   */
  @Column({ name: 'merchant_id' })
  merchantId: number;

  /**
   * 桌号ID
   */
  @Column({ name: 'table_id', nullable: true })
  tableId: number;

  /**
   * 总价
   */
  @Column({ name: 'total_price', type: 'decimal', precision: 10, scale: 2 })
  totalPrice: number;

  /**
   * 实付金额
   */
  @Column({ name: 'actual_price', type: 'decimal', precision: 10, scale: 2 })
  actualPrice: number;

  /**
   * 优惠金额
   */
  @Column({ name: 'discount_amount', type: 'decimal', precision: 10, scale: 2, default: 0 })
  discountAmount: number;

  /**
   * 订单状态：0-待支付，1-已支付，2-制作中，3-已完成，4-已取消
   */
  @Column({ type: 'tinyint', default: OrderStatusEnum.PENDING })
  status: OrderStatusEnum;

  /**
   * 备注
   */
  @Column({ length: 255, nullable: true })
  remark: string;

  /**
   * 支付方式：1-微信支付，2-支付宝
   */
  @Column({ name: 'payment_method', type: 'tinyint', nullable: true })
  paymentMethod: PaymentMethodEnum;

  /**
   * 支付时间
   */
  @Column({ name: 'payment_time', type: 'datetime', nullable: true })
  paymentTime: Date;

  /**
   * 完成时间
   */
  @Column({ name: 'finish_time', type: 'datetime', nullable: true })
  finishTime: Date;

  /**
   * 取消时间
   */
  @Column({ name: 'cancel_time', type: 'datetime', nullable: true })
  cancelTime: Date;

  /**
   * 取消原因
   */
  @Column({ name: 'cancel_reason', length: 255, nullable: true })
  cancelReason: string;

  /**
   * 是否已评价：0-否，1-是
   */
  @Column({ name: 'is_reviewed', type: 'tinyint', default: 0 })
  isReviewed: number;

  /**
   * 创建时间
   */
  @CreateDateColumn({ name: 'create_time' })
  createTime: Date;

  /**
   * 更新时间
   */
  @UpdateDateColumn({ name: 'update_time' })
  updateTime: Date;

  /**
   * 订单详情
   */
  @OneToMany(() => OrderDetail, detail => detail.order)
  details: OrderDetail[];
  
  /**
   * 支付超时时间
   */
  @Column({ name: 'payment_deadline', type: 'datetime', nullable: true })
  paymentDeadline: Date;
}